﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Homebound
{
    class Hud
    {
        private Level level;
        GraphicsDeviceManager graphics;
        SpriteFont hudFont;
        ContentManager content;
       

        // Textures
        private Texture2D lifeTexture;
        private Texture2D bar;

        // When the time remaining is less than the warning time, it blinks on the hud
        private static readonly TimeSpan WarningTime = TimeSpan.FromSeconds(30);

        public Hud(Level newLevel, GraphicsDeviceManager newGraphics, ContentManager newContent)
        {
            level = newLevel;
            graphics = newGraphics;
            content = newContent;
            loadContent();
        }

        public void loadContent()
        {
            lifeTexture = content.Load<Texture2D>("Sprites/Player/life");
            hudFont = content.Load<SpriteFont>("Fonts/Hud");
            bar = content.Load<Texture2D>("energy");
        }

        // Draw the HUD.
        public void draw(SpriteBatch spriteBatch)
        {
            // Matthew: Added SpriteBatch drawing to HUD
            spriteBatch.Begin();

            Rectangle titleSafeArea = graphics.GraphicsDevice.Viewport.TitleSafeArea;
            Vector2 hudLocation = new Vector2(titleSafeArea.X, titleSafeArea.Y);
            Vector2 center = new Vector2(titleSafeArea.X + titleSafeArea.Width / 2.0f,
                                         titleSafeArea.Y + titleSafeArea.Height / 2.0f);

            // Draw time remaining. Uses modulo division to cause blinking when the
            // player is running out of time.
            string timeString = "TIME: " + level.TimeRemaining.Minutes.ToString("00") + ":" + level.TimeRemaining.Seconds.ToString("00");

            Color timeColor;

            if (level.TimeRemaining > WarningTime ||
                level.ReachedExit ||
                (int)level.TimeRemaining.TotalSeconds % 2 == 0)
            {
                timeColor = Color.Yellow;
            }
            else
            {
                timeColor = Color.Red;
            }
            DrawLib.DrawShadowedString(spriteBatch, hudFont, timeString, hudLocation, timeColor);

            // Draw score
            float timeHeight = hudFont.MeasureString(timeString).Y;
            DrawLib.DrawShadowedString(spriteBatch, hudFont, "SCORE: " + level.Score.ToString(), hudLocation + new Vector2(0.0f, timeHeight * 1.2f), Color.Beige);

            // TODO More accurate placement of HUD elements using width measurements (rather than hardcoding values).
            // Draw Life total
            float timeWidth = hudFont.MeasureString(timeString).X;
            //DrawLib.DrawShadowedString(spriteBatch, hudFont, "HP: " + level.player.life.ToString(), hudLocation + new Vector2(timeWidth * 1.2f, 0.0f), Color.Red);
            DrawLib.DrawShadowedString(spriteBatch, hudFont, "HP: ", hudLocation + new Vector2(timeWidth * 1.2f, 0.0f), Color.Red);
            Vector2 hpBarLocation = hudLocation + new Vector2(timeWidth * 1.60f, 3f);
            Color hpBarColor = new Color(1f - ((float)level.player.life / 100f), 0.5f, 0f);
            spriteBatch.Draw(bar, new Rectangle((int)hpBarLocation.X, (int)hpBarLocation.Y, (int)(level.player.life), 20), hpBarColor);

            // Draw Lives remaining
            for ( int i = 0; i != level.player.lives; ++i )
            {
                spriteBatch.Draw(lifeTexture, new Vector2(timeWidth * 2.6f + i * 30, timeHeight * 1.2f), Color.White);
            }

            // TODO More accurate placement of HUD elements using width measurements (rather than hardcoding values).
            // Draw energy remaining.
            //DrawLib.DrawShadowedString(spriteBatch, hudFont, "Energy: " + Math.Round((level.player.CurrentEnergy * 100f), 0).ToString() + "%", hudLocation + new Vector2(timeWidth * 2.6f, 0.0f), Color.Yellow);
            DrawLib.DrawShadowedString(spriteBatch, hudFont, "Energy: ", hudLocation + new Vector2(timeWidth * 2.6f, 0.0f), Color.Yellow);
            Vector2 energyBarLocation = hudLocation + new Vector2(timeWidth * 3.40f, 3f);
            Color energyBarColor = new Color(1f - level.player.CurrentEnergy, 0.5f, 0f);
            spriteBatch.Draw(bar, new Rectangle((int)energyBarLocation.X, (int)energyBarLocation.Y, (int)(level.player.CurrentEnergy * 100f), 20), energyBarColor);

            // Draw Orb collection status
            int required = (int)(0.75 * level.totalOrbC);
            string orbStr = "ORBS: ";
            if (level.orbC >= required) // The required amount of orbs has been obtained.
            {
                int bonus = level.orbC - required;
                orbStr += level.orbC;
                orbStr += "(";
                orbStr += "+";
                orbStr += bonus;
                orbStr += ")";
            }
            else // The required amount of orbs has been not been obtained.
            {
                int needed = (required - level.orbC);
                orbStr += level.orbC;
                orbStr += "(";
                orbStr += "-";
                orbStr += needed;
                orbStr += ")";
            }
            DrawLib.DrawShadowedString(spriteBatch, hudFont, orbStr, hudLocation + new Vector2(timeWidth * 1.2f, timeHeight * 1.2f), Color.Aqua);
            
            // Matthew: Added SpriteBatch drawing to HUD
            spriteBatch.End();
        }
    }
}
